package lvxu.controller;

import lvxu.bean.Admin;
import lvxu.bean.Msg;
import lvxu.bean.User;
import lvxu.service.AdminService;
import lvxu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

//@SessionAttributes("user")
@Controller
public class AdminController {
    @Autowired
    AdminService adminService;
    //注，在没有写后台登录功能时我用的直接跳转后台，该方法对应用户登录那里的a标签
    //页面跳转到admin.jsp
    @RequestMapping(value = "/admin",method = RequestMethod.GET)
    @ResponseBody
    public ModelAndView service(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("admin");
//        List<User> users = userService.selectUsers();
        System.out.println("我是service");
        return modelAndView;
    }
    //页面跳转到login.jsp
    @RequestMapping(value = "/login",method = RequestMethod.GET)
    @ResponseBody
    public ModelAndView exit(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("login");
        System.out.println("成功");
        return modelAndView;
    }
    //用户登录功能
    @RequestMapping(value = "/admin",method = RequestMethod.POST)
    @ResponseBody
    public ModelAndView login(Admin admin){

        System.out.println(admin);
        boolean login = adminService.login(admin);
        ModelAndView modelAndView = new ModelAndView();

        if (login){
            modelAndView.setViewName("admin");
            List<Admin> admin1 = adminService.selectAdimin(admin.getAdminname());
            modelAndView.addObject("admin",admin1);
        }else {
            modelAndView.setViewName("login");
            Msg message = Msg.fail().add("message", "用户名或密码错误！！！");
            modelAndView.addObject("error",message);
        }
        //导航页码数
//        PageInfo<CollectList> pageInfo = new PageInfo<>(collectlistList);
//        return Msg.success().add("collect",pageInfo);
        return modelAndView;
    }

}
